Public Class M_Tenant
    Dim dtable As New DataTable
    Dim dadapter As New OleDb.OleDbDataAdapter
    Private a, t0, t1, t2, t3, t4, t5, t6, t7, t8, t9 As String
    Private d1 As Date
    Private o1, o2, o3, o4 As Boolean
    Private nw As Boolean
    Private Function cek_null() As Boolean
        If Nama.Text = "" Then
            cek_null = True
            'Else
            '    If ALamat.Text = "" Then
            '        cek_null = True
            '    Else
            '        If Alamat2.Text = "" Then
            '            cek_null = True
            '        Else
            '            If Tlp.Text = "" Then
            '                cek_null = True
            '            Else
            '                If HP.Text = "" Then
            '                    cek_null = True
            '                Else
            '                    cek_null = False
            '                End If
            '            End If
            '        End If
            '    End If
        End If
    End Function
    Private Sub save()
        Dim query As String
        'Dim i As Integer
        query = ""
        If Not cek_null() Then
            If nw Then
                'qry = "insert into m_tenant (nama,alamat1,alamat2,no_tlp,no_hp,no_tlp_kios" & _
                '",no_ktp,ktp_expired,no_npwp,alamat_npwp,akte_nikah,surat_ganti_nama," & _
                '"perjanjian_kawin,kartu_keluarga) values ('" & Nama.Text & "','" & _
                'ALamat.Text & "','" & Alamat2.Text & "','" & Tlp.Text & "','" & _
                'HP.Text & "','" & TlpKios.Text & "','" & KTPNo.Text & "','" & _
                'KTPExp.Text & "','" & NPWPNo.Text & "','" & NPWPAlm.Text & "','"
                'If AN.Checked Then
                '    qry = qry + "1,"
                'Else
                '    qry = qry + "0,"
                'End If
                'If PN.Checked Then
                '    qry = qry + "1,"
                'Else
                '    qry = qry + "0,"
                'End If
                'If GN.Checked Then
                '    qry = qry + "1,"
                'Else
                '    qry = qry + "0,"
                'End If
                'If KK.Checked Then
                '    qry = qry + "1)"
                'Else
                '    qry = qry + "0)"
                'End If
                'nw = False
                query = add_query(query, "nama", Nama.Text, "s")
                query = add_query(query, "alamat1", ALamat.Text, "s")
                query = add_query(query, "alamat2", Alamat2.Text, "s")
                query = add_query(query, "no_tlp", Tlp.Text, "s")
                query = add_query(query, "no_h", HP.Text, "s")
                query = add_query(query, "no_tlp_kios", TlpKios.Text, "s")
                query = add_query(query, "no_ktp", KTPNo.Text, "s")
                query = add_query(query, "ktp_expired", KTPExp.Text, "d")
                query = add_query(query, "no_npwp", NPWPNo.Text, "s")
                query = add_query(query, "alamat_npwp", NPWPAlm.Text, "s")
                query = add_query(query, "akte_nikah", AN.Checked, "b")
                query = add_query(query, "surat_ganti_nama", GN.Checked, "b")
                query = add_query(query, "perjanjian_kawin", PN.Checked, "b")
                query = add_query(query, "kartu_keluarga", KK.Checked, "b")
                If query = "" Then
                    MsgBox("Save Failed", MsgBoxStyle.Exclamation, "Error")
                Else
                    query = "insert into m_tenant (" & query & ")"
                End If
            Else
                'qry = "update m_tenant set "
                'i = 0
                'If Nama.Text <> t1 Then
                '    qry = qry + "nama='" & Nama.Text & "'"
                '    i += 1
                'End If
                'If ALamat.Text <> t2 Then
                '    If i > 0 Then
                '        qry = qry + ","
                '    End If
                '    i += 1
                '    qry = qry + "alamat1='" & ALamat.Text & "'"
                'End If
                'If Alamat2.Text <> t3 Then
                '    If i > 0 Then
                '        qry = qry + ","
                '    End If
                '    i += 1
                '    qry = qry + "alamat2='" & Alamat2.Text & "'"
                'End If
                'If Tlp.Text <> t4 Then
                '    If i > 0 Then
                '        qry = qry + ","
                '    End If
                '    i += 1
                '    qry = qry + "no_tlp='" & Tlp.Text & "'"
                'End If
                'If HP.Text <> t5 Then
                '    If i > 0 Then
                '        qry = qry + ","
                '    End If
                '    i += 1
                '    qry = qry + "no_hp='" & HP.Text & "'"
                'End If
                'If TlpKios.Text <> t6 Then
                '    If i > 0 Then
                '        qry = qry + ","
                '    End If
                '    i += 1
                '    qry = qry + "no_tlp_kios='" & TlpKios.Text & "'"
                'End If
                'If KTPNo.Text <> t7 Then
                '    If i > 0 Then
                '        qry = qry + ","
                '    End If
                '    i += 1
                '    qry = qry + "no_ktp='" & KTPNo.Text & "'"
                'End If
                'If KTPExp.Text <> d1 Then
                '    If i > 0 Then
                '        qry = qry + ","
                '    End If
                '    i += 1
                '    qry = qry + "ktp_expired='" & KTPExp.Text & "'"
                'End If
                'If NPWPNo.Text <> t8 Then
                '    If i > 0 Then
                '        qry = qry + ","
                '    End If
                '    i += 1
                '    qry = qry + "no_npwp='" & NPWPNo.Text & "'"
                'End If
                'If NPWPAlm.Text <> t9 Then
                '    If i > 0 Then
                '        qry = qry + ","
                '    End If
                '    i += 1
                '    qry = qry + "alamat_npwp='" & NPWPAlm.Text & "'"
                'End If
                'If AN.Checked <> o1 Then
                '    If i > 0 Then
                '        qry = qry + ","
                '    End If
                '    i += 1
                '    If AN.Checked Then
                '        qry = qry + "akte_nikah=1"
                '    Else
                '        qry = qry + "akte_nikah=0"
                '    End If
                'End If
                'If GN.Checked <> o3 Then
                '    If i > 0 Then
                '        qry = qry + ","
                '    End If
                '    i += 1
                '    If GN.Checked Then
                '        qry = qry + "surat_ganti_nama=1"
                '    Else
                '        qry = qry + "surat_ganti_nama=0"
                '    End If
                'End If
                'If PN.Checked <> o2 Then
                '    If i > 0 Then
                '        qry = qry + ","
                '    End If
                '    i += 1
                '    If AN.Checked Then
                '        qry = qry + "perjanjian_kawin=1"
                '    Else
                '        qry = qry + "perjanjian_kawin=0"
                '    End If
                'End If
                'If KK.Checked <> 4 Then
                '    If i > 0 Then
                '        qry = qry + ","
                '    End If
                '    i += 1
                '    If KK.Checked Then
                '        qry = qry + "kartu_keluarga=1"
                '    Else
                '        qry = qry + "kartu_keluarga=0"
                '    End If
                'End If
                'qry = qry + " where tenant_id =" & t0

                query = edit_query(query, "nama", t1, Nama.Text, "s")
                query = edit_query(query, "alamat1", t2, ALamat.Text, "s")
                query = edit_query(query, "alamat2", t3, Alamat2.Text, "s")
                query = edit_query(query, "no_tlp", t4, Tlp.Text, "s")
                query = edit_query(query, "no_h", t5, HP.Text, "s")
                query = edit_query(query, "no_tlp_kios", t6, TlpKios.Text, "s")
                query = edit_query(query, "no_ktp", t7, KTPNo.Text, "s")
                query = edit_query(query, "ktp_expired", d1, KTPExp.Text, "d")
                query = edit_query(query, "no_npwp", t8, NPWPNo.Text, "s")
                query = edit_query(query, "alamat_npwp", t9, NPWPAlm.Text, "s")
                query = edit_query(query, "akte_nikah", o1, AN.Checked, "b")
                query = edit_query(query, "surat_ganti_nama", o2, GN.Checked, "b")
                query = edit_query(query, "perjanjian_kawin", o3, PN.Checked, "b")
                query = edit_query(query, "kartu_keluarga", o4, KK.Checked, "b")
                If query = "" Then
                    MsgBox("Save Failed", MsgBoxStyle.Exclamation, "Error")
                Else
                    query = "update m_tenant set " & query & " where id=" & t0
                End If
            End If
            exec_qr(query)
            MsgBox("Data berhasil di simpan", MsgBoxStyle.Information, "Complete")
            If complite() Then
                exec_qr("update m_tenant set complete=true where tenant_id=" & t0)
            End If
            load_grid("select * from m_tenant")
            set_param(t0)
        Else
            MsgBox("Harap mengisi semua data", MsgBoxStyle.Information, "Isi Data")
        End If
    End Sub
    Private Sub load_grid(ByVal sql As String)
        dtable.Clear()
        open_con()
        dadapter = New OleDb.OleDbDataAdapter(sql, DbCon)
        dadapter.Fill(dtable)
        DataGridView1.DataSource = dtable
        DataGridView1.Columns(0).Visible = False
        'DataGridView1.Columns(2).Visible = False
        'DataGridView1.Columns(4).HeaderText = "Level"
        DataGridView1.RowHeadersVisible = False
        DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridView1.AutoResizeColumns()
        DataGridView1.ReadOnly = True
        DbCon.Close()
    End Sub
    Private Sub detail(ByVal id)
        open_db("select * from m_tenant where tenant_id=" & id)
        While Read.Read()
            Me.ID.Text = Read(0)
            Me.Nama.Text = Read(1)
            Me.ALamat.Text = isnull(Read(2))
            Me.Alamat2.Text = isnull(Read(3))
            Me.Tlp.Text = isnull(Read(4))
            Me.HP.Text = isnull(Read(5))
            Me.TlpKios.Text = isnull(Read(6))
            Me.KTPNo.Text = isnull(Read(7))
            Me.KTPExp.Text = isnull(Read(8))
            Me.NPWPNo.Text = isnull(Read(9))
            Me.NPWPAlm.Text = isnull(Read(10))
            Me.AN.Checked = isnull(Read(11))
            Me.PN.Checked = isnull(Read(12))
            Me.GN.Checked = isnull(Read(13))
            Me.KK.Checked = isnull(Read(14))
        End While
        close_db()
        set_param(id)
    End Sub
    Private Sub set_param(ByVal id)
        t0 = id
        t1 = Nama.Text
        t2 = ALamat.Text
        t3 = Alamat2.Text
        t4 = Tlp.Text
        t5 = HP.Text
        t6 = TlpKios.Text
        t7 = KTPNo.Text
        t8 = NPWPNo.Text
        t9 = NPWPAlm.Text
        d1 = KTPExp.Value.ToShortDateString
        o1 = AN.Checked
        o2 = PN.Checked
        o3 = GN.Checked
        o4 = KK.Checked
    End Sub
    Private Function cek_change() As Boolean
        If Nama.Text <> t1 Then
            Return True
        Else
            If ALamat.Text <> t2 Then
                Return True
            Else
                If Alamat2.Text <> t3 Then
                    Return True
                Else
                    If Tlp.Text <> t4 Then
                        Return True
                    Else
                        If HP.Text <> t5 Then
                            Return True
                        Else
                            If TlpKios.Text <> t6 Then
                                Return True
                            Else
                                If KTPNo.Text <> t7 Then
                                    Return True
                                Else
                                    If NPWPNo.Text <> t8 Then
                                        Return True
                                    Else
                                        If NPWPAlm.Text <> t9 Then
                                            Return True
                                        Else
                                            If KTPExp.Text <> d1 Then
                                                Return True
                                            Else
                                                If AN.Checked <> o1 Then
                                                    Return True
                                                Else
                                                    If PN.Checked <> o2 Then
                                                        Return True
                                                    Else
                                                        If GN.Checked <> o3 Then
                                                            Return True
                                                        Else
                                                            If KK.Checked <> o4 Then
                                                                Return True
                                                            Else
                                                                Return False
                                                            End If
                                                        End If
                                                    End If
                                                End If
                                            End If
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End Function

    Private Sub M_Tenant_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        load_grid("select * from m_tenant")
        nw = True
        set_param("")
    End Sub

    Private Sub ToolStripButton8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton8.Click
        Me.Close()
    End Sub

    Private Sub ToolStripButton7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton7.Click
        DataGridView1.CurrentCell = DataGridView1.Item(1, DataGridView1.RowCount - 1)
        detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
    End Sub

    Private Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton6.Click
        Dim i As Integer
        'If DataGridView1.CurrentRow.Index = 0 Then ' Is Nothing Then
        'i = -1
        ' Else
        i = DataGridView1.CurrentRow.Index
        ' End If
        If i + 1 < DataGridView1.RowCount Then
            DataGridView1.CurrentCell = DataGridView1.Item(1, i + 1)
            detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
        End If
    End Sub

    Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
        Dim i As Integer
        'If DataGridView1.CurrentRow Is Nothing Then
        'i = 1
        'Else
        i = DataGridView1.CurrentRow.Index
        ' End If
        If i - 1 >= 0 Then
            DataGridView1.CurrentCell = DataGridView1.Item(1, i - 1)
            detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
        End If
    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        DataGridView1.CurrentCell = DataGridView1.Item(1, 0)
        detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        Dim x
        Dim sql As String
        x = MsgBox("Apakah yakin akan menghapus data?", MsgBoxStyle.YesNo, "Detele Data")
        If x = 6 Then
            sql = "delete from m_login where tenant_id=" & t0
            exec_qr(sql)
            load_grid("select * from m_tenant")
        End If
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        save()
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        nw = True
        Nama.Text = ""
        ALamat.Text = ""
        Alamat2.Text = ""
        Tlp.Text = ""
        HP.Text = ""
        TlpKios.Text = ""
        KTPNo.Text = ""
        NPWPNo.Text = ""
        NPWPAlm.Text = ""
        KTPExp.Text = ""
        AN.Checked = False
        PN.Checked = False
        GN.Checked = False
        KK.Checked = False
        Nama.Focus()
        set_param("")
    End Sub
    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Dim jwb
        If cek_change() Then
            jwb = MsgBox("Data telah berubah, apakah data akan di simpan?", MsgBoxStyle.YesNo, "Save Changes")
            If jwb = 6 Then
                save()
            End If
        End If
        detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
        nw = False
    End Sub
    Private Function complite() As Boolean
        If Nama.Text = "" Then
            complite = False
        Else
            If ALamat.Text = "" Then
                complite = False
            Else
                If Alamat2.Text = "" Then
                    complite = False
                Else
                    If Tlp.Text = "" Then
                        complite = False
                    Else
                        If HP.Text = "" Then
                            complite = False
                        Else
                            If TlpKios.Text = "" Then
                                complite = False
                            Else
                                If KTPNo.Text = "" Then
                                    complite = False
                                Else
                                    If NPWPNo.Text = "" Then
                                        complite = False
                                    Else
                                        If NPWPAlm.Text = "" Then
                                            complite = False
                                        Else
                                            If KTPExp.Text = "" Then
                                                complite = False
                                            Else
                                                If AN.Checked = True Then
                                                    complite = False
                                                Else
                                                    If PN.Checked = True Then
                                                        complite = False
                                                    Else
                                                        If GN.Checked = True Then
                                                            complite = False
                                                        Else
                                                            If KK.Checked = True Then
                                                                complite = False
                                                            Else
                                                                complite = True
                                                            End If
                                                        End If
                                                    End If
                                                End If
                                            End If
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End Function
    Private Sub DataGridView1_Keyup(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        If e.KeyCode = Keys.Down Then
            If DataGridView1.CurrentRow.Index + 1 < DataGridView1.RowCount Then
                detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index + 1).Value)
            End If
        End If
        If e.KeyCode = Keys.Up Then
            If DataGridView1.CurrentRow.Index - 1 >= 0 Then
                detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index - 1).Value)
            End If
        End If
    End Sub
    Private Sub pick()
        ToolStripButton1.Enabled = False
        ToolStripButton2.Enabled = False
        ToolStripButton3.Enabled = False
        Button1.Visible = True
    End Sub
    Public Function getselection(ByVal text As String) As String
        If text <> "" Then
            detail(text)
        End If
        pick()
        Me.ShowDialog()
        Return a
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        T_id = ID.Text
        a = Nama.Text
        Me.Close()
    End Sub

    Private Sub ToolStripButton9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton9.Click
        If SearchText.Text.Length = 0 Then
            load_grid("select * from m_tenant")
        Else
            load_grid("select * from m_tenant where " & SearchMode.Text & " like '%" & SearchText.Text & "%'")
        End If
    End Sub
End Class